﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace MyGames
{
    class EnemyProjectileManager
    {
        static Player player;
        static ParticleManager particles;
        List<EnemyProjectile> enemyProjects = new List<EnemyProjectile>();

        #region Constructor
        public EnemyProjectileManager(Player player, ParticleManager particles)
        {
            EnemyProjectileManager.player = player;
            EnemyProjectileManager.particles = particles;
        }
        #endregion

        #region Public methods
        public void AddEnemyProject(EnemyProjectile project)
        {
            project.StartProject();
            enemyProjects.Add(project);
        }

        public void ClearAll()
        {
            enemyProjects.Clear();
        }
        #endregion

        #region Update and Draw
        public void Update(GameTime gameTime)
        {
            for (int i = 0; i < enemyProjects.Count; i++)
            {
                // Remove finish projects
                if (!enemyProjects[i].IsFlying)
                {
                    enemyProjects.RemoveAt(i);
                    continue;
                }

                // Check collision
                if (enemyProjects[i].IsCollideWith(player, false))
                {
                    // Add particle base on Attack type and element
                    particles.AddParticle(enemyProjects[i].AttackType, player, false);

                    player.GetDamage(enemyProjects[i].Value);
                    enemyProjects[i].StopProject();
                }

                enemyProjects[i].Update(gameTime);
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (EnemyProjectile project in enemyProjects)
                project.Draw(spriteBatch);
        }
        #endregion
    }
}
